package com.myres.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.orders.db.OrderService;
import com.myres.util.GZIPTool;
import com.myres.model.Order;
import java.util.List;

public class getMyOrders extends ServiceServlet {

	/**
	 * Constructor of the object.
	 */
	public getMyOrders() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
    @Override
	public void doRequest(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
    	int uid=Integer.parseInt(request.getParameter("uid"));
		List<Order> orders=new OrderService().getOrders(uid);
		
		StringBuilder builder=new StringBuilder();
		builder.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
		builder.append("<response>");
		builder.append("<orders>");
		int i=0;
		for(Order order:orders){
			i++;
			if(i>15) break;
		builder.append("<order>");
		builder.append("<o_id>");
		builder.append(order.getId());
		builder.append("</o_id>");
		builder.append("<o_place_time>");
		builder.append(order.getOrderTime());
		builder.append("</o_place_time>");
		builder.append("<o_status>");
		builder.append(order.getState());
		builder.append("</o_status>");
		builder.append("<o_mobile>");
		builder.append(order.getReceiverPhone());
		builder.append("</o_mobile>");
		builder.append("<o_payment>");
		builder.append(order.getPayment());
		builder.append("</o_payment>");
		builder.append("<o_place>");
		builder.append(order.getSendToPlace());
		builder.append("</o_place>");
		builder.append("</order>");
		//break;
		}
		builder.append("</orders>");
		builder.append("</response>");
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain;charset=utf-8");
		String content=builder.toString();
		
		log(content);
		//content=new GZIPTool().compress(content);
    	
    	//log(content);
    	//content=java.net.URLEncoder.encode(content,"GBK");
    	
    	//log("out="+content);
		response.getWriter().write(content);
		response.getWriter().flush();
		
	}
	public void init() throws ServletException {
		// Put your code here
	}

}
